﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Random</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="../css/commands.css" rel="stylesheet" type="text/css">
<link href="../css/print.css" rel="stylesheet" type="text/css" media="print">
</head>
<body>

<h1>Random</h1>

<p>产生一个伪随机数。</p>

<p class="CommandSyntax">Random, OutputVar [, Min, Max]<br>
    Random, , NewSeed</p>
<h3>参数 </h3>
<table border="1" width="100%" cellspacing="0" cellpadding="3" bordercolor="#C0C0C0">
  <tr>
    <td>OutputVar</td>
    <td>存储结果的变量名。被存储的浮点数的格式由 <a href="SetFormat.htm">SetFormat</a> 决定。</td>
  </tr>
  <tr>
    <td width="15%">Min</td>
    <td width="85%">可以产生的最小的数。可以是负数，浮点数，或者一个 <a href="../Variables.htm#Expressions">expression</a> (表达式)。缺省情况下，产生的最小的数是 0 。
      允许的最小整数是 -2147483648，浮点数没有限制。</td>
  </tr>
  <tr>
    <td>Max</td>
    <td>可以产生的最大的数。可以是负数，浮点数，或者一个 <a href="../Variables.htm#Expressions">expression</a> (表达式)。如果省略，缺省情况下，产生的最大的数是 2147483647 (这也是允许的最大整数 -- 但是浮点数没有限制)。</td>
  </tr>
  <tr>
    <td>NewSeed</td>
    <td><p>此模式通过 <em>NewSeed</em> (可以是一个 <a href="../Variables.htm#Expressions">expression</a> (表达式)) 对随机数产生器重新定义种子。这对随后产生的随机数都有影响。<em>NewSeed</em> 应当是介于 0 和 4294967295 (0xFFFFFFFF)之间的整数。重新定义种子能够提高所产生随机数的质量和安全性，尤其当 <em>NewSeed</em> 是一个真正的随机数而不是像伪随机数一样质量欠佳时更是这样。通常，种子只需要重新定义一次即可。</p>
      <p>如果脚本中从来没有重定义过种子，种子使用一个 64 位值的低 32 位作为开始。这个 64 位的值是从1601年1月1日开始100纳秒的数目。这个低 32 位值从 0 变化至 4294967295，其中100毫秒累计 ~7.2 分钟。</p></td>
  </tr>
</table>

<h3>注意</h3>
<p>此命令产生一个伪随机数，即一个模拟真正随机数的数字，但是它实际上是一个基于复杂方程式的使得决定或者猜测下一个数字极为困难的数字。</p>
<p>如果 <em>Min</em> 或者 <em>Max</em> 其中之一被设置为十进制浮点数，最后的结果将是由 <a href="SetFormat.htm">SetFormat</a>  决定格式的浮点数。否则，结果将是一个整数。</p>
<p>结果是浮点数的局限：1) 只有其中 32位用于产生变量；2) 它偶尔也会比指定的 <em>Max</em> 略微大一点点(这是由浮点数内在的不精确性导致的)。</p>
<h3>相关命令</h3>
<p><a href="SetFormat.htm">SetFormat</a></p>
<h3>示例</h3>
<pre class="NoIndent">Random, rand, 1, 10
Random, rand, 0.0, 1.0</pre>
<h3>关于原始资料的注释</h3>
<P>此函数使用了 Mersenne Twister 随机数产生器，MT19937, 原作者：Takuji Nishimura , Matsumoto, Shawn Cokus, Matthe Bellew, Isaku Wada.</P>
<P>Mersenne Twister 是一个用于产生随意数的算法。它在考虑其他随机数产生器的基础上被设计出来。它的循环周期，2<SUP>19937</SUP>-1，和它的等分布顺序，623 dimensions(维度)，都非常之大。与此同时，这个产生器非常快，它避免了乘法和除法，并且从缓存和管道中得益。想了解更多，请访问原作者的网站 www.math.keio.ac.jp/~matumoto/emt.html</P>
<P><em>(译者注：以下是关于算法的法律声明与使用限制，一般不允许翻译，但是这个没说，以下就不翻译了，不影响大家对此命令的使用^_^)</em></P>
<P>Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved.</P>
<P>Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:</P>
<OL>
  <LI>Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.</LI>
  <LI>Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.</LI>
  <LI>The names of its contributors may not be used to endorse or promote products
    derived from this software without specific prior written permission.</LI>
</OL>
<P class="J">THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</P>
<P>Do NOT use for CRYPTOGRAPHY without securely hashing several returned values
  together, otherwise the generator state can be learned after reading 624 consecutive
  values.</P>
<P>When you use this, send an email to: matumoto@math.keio.ac.jp with an appropriate
  reference to your work. It would be nice to CC: rjwagner@writeme.com and Cokus@math.washington.edu
  when you write.</P>
<P><I>This above has been already been done for AutoHotkey, but if you use the Random command in a publicly distributed application,
    consider sending an e-mail to the above people to thank them.</I></P>

</body>
</html>
